package schema

import (
	"time"

	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
)

// User holds the schema definition for the User entity.
type User struct {
	ent.Schema
}

// Fields of the User.
func (User) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("id").
			Positive().
			Unique().
			Immutable().
			Comment("用户ID"),
		field.String("username").
			Unique().
			NotEmpty().
			MaxLen(50).
			Comment("用户名"),
		field.String("password").
			NotEmpty().
			MaxLen(255).
			Sensitive().
			Comment("密码"),
		field.String("email").
			Unique().
			NotEmpty().
			MaxLen(100).
			Comment("邮箱"),
		field.String("display_name").
			NotEmpty().
			MaxLen(100).
			Comment("显示名称"),
		field.String("avatar_url").
			Optional().
			MaxLen(255).
			Comment("头像URL"),
		field.Enum("status").
			Values("online", "offline", "away", "busy").
			Default("offline").
			Comment("在线状态"),
		field.String("department").
			Optional().
			MaxLen(100).
			Comment("部门"),
		field.String("position").
			Optional().
			MaxLen(100).
			Comment("职位"),
		field.Time("created_at").
			Default(time.Now).
			Immutable().
			Comment("创建时间"),
		field.Time("updated_at").
			Default(time.Now).
			UpdateDefault(time.Now).
			Comment("更新时间"),
	}
}

// Edges of the User.
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("created_conversations", Conversation.Type).
			Comment("用户创建的会话"),
		edge.To("memberships", ConversationMember.Type).
			Comment("用户的会话成员关系"),
		edge.To("sent_messages", Message.Type).
			Comment("用户发送的消息"),
		edge.To("read_statuses", MessageReadStatus.Type).
			Comment("用户的消息已读状态"),
		edge.To("attendance_records", AttendanceRecord.Type).
			Comment("用户的考勤记录"),
		edge.To("user_departments", UserDepartment.Type),
		edge.To("created_activities", Activity.Type).
			Comment("该用户创建的活动"),
		edge.To("uploaded_attachments", ActivityAttachment.Type).
			Comment("该用户上传的附件"),
		edge.To("activity_participations", ActivityParticipant.Type).
			Comment("该用户参与的活动记录"),
		edge.To("reviews", ActReview.Type).
			Comment("该用户参与的活动评论"),
	}
}

// Indexes of the User.
func (User) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("username"),
		index.Fields("email"),
	}
}
